
export default {
  //表格字符串截取
  setSubstring(data) {
    return data.length >= 15 ? `${data.boxType.substring(0, 15)}...` : data
  },
  //重构树
  treeDatas(data) {
    //定义一个空数组存放重构后得数组
    let treeDetas = [];
    // 一级循环出拥有得一级菜单
    data.forEach((item, i) => {
      // 添加为想要得数组
      treeDetas.push({
        label: item.title,
        value: item.key,
        key: item.key,
        children: [],
      });
      // 如果一级菜单有儿子
      if (item.children) {
        // 循环一级菜单得儿子
        item.children.forEach((v) => {
          treeDetas[i].children.push({
            label: v.title,
            value: v.key,
            key: v.key,
            children: [],
          });
          if (v.children) {
            let len = treeDetas[i].children.length;
            let children = [...v.children];
            children.forEach((item) => {
              treeDetas[i].children[len - 1].children.push({
                label: item.title,
                value: item.key,
                key: item.key,
              });
            });
          }
        });
      }
    });
    return treeDetas;
  },
  exportFile(name, res) {
    const blob = new Blob([res], {
      type: "application/vnd.ms-excel;",
    });
    const fileName = `${name}.xlsx`;
    const linkNode = document.createElement("a");
    linkNode.download = fileName; //a标签的download属性规定下载文件的名称
    linkNode.style.display = "none";
    linkNode.href = URL.createObjectURL(blob); //生成一个Blob URL
    document.body.appendChild(linkNode);
    linkNode.click(); //模拟在按钮上的一次鼠标单击
    URL.revokeObjectURL(linkNode.href); // 释放URL 对象
    document.body.removeChild(linkNode);
  },
  exportCsv(name, res) {
    const blob = new Blob([res], {
      type: "text/tab-separated-values;",
    });
    const fileName = `${name}.csv`;
    const linkNode = document.createElement("a");
    linkNode.download = fileName; //a标签的download属性规定下载文件的名称
    linkNode.style.display = "none";
    linkNode.href = URL.createObjectURL(blob); //生成一个Blob URL
    document.body.appendChild(linkNode);
    linkNode.click(); //模拟在按钮上的一次鼠标单击
    URL.revokeObjectURL(linkNode.href); // 释放URL 对象
    document.body.removeChild(linkNode);
  },
  exportPDF(name, res) {
    let blob = new Blob([res], {
      type: "application/pdf;charset-UTF-8",
    });
    let link = document.createElement("a");
    let fileName = `${name}.pdf`; //文件命和后缀
    link.style.display = "none";
    link.href = window.URL.createObjectURL(blob);
    link.download = decodeURIComponent(fileName);
    link.click();
  },
  exportZIP(name, res) {
    let blob = new Blob([res], {
      type: "application/zip",
    });
    let link = document.createElement("a");
    let fileName = `${name}.zip`; //文件命和后缀
    link.style.display = "none";
    link.href = window.URL.createObjectURL(blob);
    link.download = decodeURIComponent(fileName);
    link.click();
  }

}